package com.starrin.trashtalk;

import com.starrin.trashtalk.dao.MatchDAO;
import com.starrin.trashtalk.dao.MatchDAOImpl;
import com.starrin.trashtalk.dao.PlayerDAO;
import com.starrin.trashtalk.dao.PlayerDAOImpl;
import com.starrin.trashtalk.model.Player;
import com.starrin.trashtalk.model.SingleMatch;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by larsstarrin on 01/11/14.
 */
@ManagedBean
@ViewScoped
public class HeadToHeadBean implements Serializable{

    private String player1Name;
    private Long player1Id;

    private String player2Name;
    private Long player2Id;

    private PlayerDAO playerDAO = new PlayerDAOImpl();
    private MatchDAO matchDAO = new MatchDAOImpl();

    private List<Player> selectableHomePlayers;
    private List<Player> selectableAwayPlayers;

    private List<SingleMatch> matches;

    private int player1Wins = 0;
    private int player2Wins = 0;

    @PostConstruct
    public void init() {
        selectableHomePlayers = playerDAO.findAllPlayers();
        selectableAwayPlayers = new ArrayList<>(selectableHomePlayers);
    }

    public void submit() {
        if (player1Name != null && player2Name != null) {
            player1Wins = 0;
            player2Wins = 0;

            for (Player homePlayer : selectableHomePlayers) {
                if (homePlayer.fullName().equals(player1Name)) {
                    player1Id = homePlayer.getId();
                    break;
                }
            }

            for (Player awayPlayer : selectableAwayPlayers) {
                if (awayPlayer.fullName().equals(player2Name)) {
                    player2Id = awayPlayer.getId();
                    break;
                }
            }
            matches = matchDAO.findMatchesBetween(player1Id, player2Id);

            for (SingleMatch match : matches) {
                if(match.getWinner() != null){
                    if (match.getWinner().getId().equals(player1Id)) {
                        player1Wins++;
                    } else if (match.getWinner().getId().equals(player2Id)) {
                        player2Wins++;
                    }
                }
            }
        }
    }

    public String getPlayer1Name() {
        return player1Name;
    }

    public void setPlayer1Name(String player1Name) {
        this.player1Name = player1Name;
    }

    public String getPlayer2Name() {
        return player2Name;
    }

    public void setPlayer2Name(String player2Name) {
        this.player2Name = player2Name;
    }

    public List<Player> getSelectableHomePlayers() {
        return selectableHomePlayers;
    }

    public void setSelectableHomePlayers(List<Player> selectableHomePlayers) {
        this.selectableHomePlayers = selectableHomePlayers;
    }

    public List<Player> getSelectableAwayPlayers() {
        return selectableAwayPlayers;
    }

    public void setSelectableAwayPlayers(List<Player> selectableAwayPlayers) {
        this.selectableAwayPlayers = selectableAwayPlayers;
    }

    public int getPlayer1Wins() {
        return player1Wins;
    }

    public void setPlayer1Wins(int player1Wins) {
        this.player1Wins = player1Wins;
    }

    public int getPlayer2Wins() {
        return player2Wins;
    }

    public void setPlayer2Wins(int player2Wins) {
        this.player2Wins = player2Wins;
    }

    public List<SingleMatch> getMatches() {
        return matches;
    }

    public void setMatches(List<SingleMatch> matches) {
        this.matches = matches;
    }

    public List<String> selectablePlayerNames(String query) {
        List<String> names = new ArrayList<>();
        for (Player player : selectableAwayPlayers) {
            if (player.fullName().toLowerCase().startsWith(query.toLowerCase())) {
                names.add(player.fullName());
            }
        }
        return names;
    }
}
